package com.yisu.expressway.driver_revelation.activity;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.app.Activity;
import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.kogitune.activity_transition.ActivityTransition;
import com.kogitune.activity_transition.ActivityTransitionLauncher;
import com.kogitune.activity_transition.ExitActivityTransition;
import com.yisu.expressway.R;
import com.yisu.expressway.activity.BaseActivity;
import com.yisu.expressway.application.ExApplication;
import com.yisu.expressway.databinding.ActivityImgViewBinding;

public class ImgViewActivity extends BaseActivity {

    private ExitActivityTransition exitTransition;
    ActivityImgViewBinding binding;

    private final static String ORIGIN_IMG = "origin_img";
    private final static String IMG_URL = "img_url";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this,R.layout.activity_img_view);
        initView(getIntent(),savedInstanceState);
    }

    private void initView(Intent intent,Bundle savedInstanceState){
        String imgPath = intent.getStringExtra(IMG_URL);
        Bitmap originBitmap = intent.getParcelableExtra(ORIGIN_IMG);
        Drawable originDrawable = null;
        if(originBitmap != null){
            originDrawable = new BitmapDrawable(originBitmap);
        }else {
            originDrawable = getResources().getDrawable(R.drawable.ic_default_home_banner);
        }

        binding.ivCover.enable();
        Glide.with(ExApplication.getInstance().getApplicationContext())
                .load(imgPath)
                .asBitmap()
                .placeholder(originDrawable)
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .animate(R.anim.fade_in_image)
                .into(binding.ivCover);

        exitTransition = ActivityTransition
                .with(intent)
                .to(binding.ivCover)
                .enterListener(new AnimatorListenerAdapter() {
                    @Override
                    public void onAnimationEnd(Animator animation) {
                    }

                    @Override
                    public void onAnimationStart(Animator animation) {

                    }
                })
                .start(savedInstanceState);
    }

    public static void launch(Activity callActivity, String imgPath, View targetView){
        final Intent intent = new Intent(callActivity, ImgViewActivity.class);
        intent.putExtra(IMG_URL,imgPath);
        Drawable drawable = ((ImageView)targetView).getDrawable();
        if(drawable != null) {
            BitmapDrawable bd = (BitmapDrawable) drawable;
            intent.putExtra(ORIGIN_IMG, bd.getBitmap());
        }

        ActivityTransitionLauncher
                .with(callActivity)
                .from(targetView)
                .launch(intent);

    }

    public void onExitActivity(View view){
        onBackPressed();
    }

    @Override
    public void onBackPressed(){
        exitTransition.exit(this);
    }
}
